import { Injectable } from '@nestjs/common';
import { Strategy, StrategyOptions, ExtractJwt } from 'passport-jwt'
import { PassportStrategy } from '@nestjs/passport'
import { ConfigService } from '@nestjs/config'

@Injectable()
export class AuthJwtStrategy extends PassportStrategy(Strategy, "auth-jwt") {
  constructor(private readonly configService: ConfigService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: configService.get('JWT.publicKey'),
      algorithms: ['RS256']
    } as StrategyOptions)
  }

  async validate(payload: any) {
    const { id, username } = payload
    return {
      id,
      username
    }
  }
}

